<template>
  <div class="detail-page">
    <van-nav-bar
      left-text="返回"
      @click-left="$router.back()"
      fixed
      title="面经详细"
    />
   <header class="header">
     <h1>{{ one.stem }}</h1>
      <p>
        {{ one.createdAt }} | {{ one.views }} 浏览量 |
        {{ one.likeCount }} 点赞数
      </p>
      <p>
        <img :src="one.avatar" alt="" />
        <span>{{ one.creator }}</span>
      </p>
    </header>
    <main class="body" v-html="one.content"></main>
    <div class="opt" >
      <!-- 点击 -->
      <!-- 1.点击后，该文章真的就被点赞！后台数据肯定变了 -->
      <!-- 2 ajax的请求！第一次：点赞!第二次取消点赞 -->
      <van-icon :class="{active:one.likeFlag}" name="like-o" @click ="like"/>
      <van-icon :class="{active:one.collectFlag}" name="star-o" @click="collect" />
    </div>
  </div>
</template>

<script>
export default {
  name: 'detail-page',
  data () {
    return {
      one: {}
    }
  },
  async created () {
    // 3 组件展示出来后：获取id
    const id = this.$route.params.id
    // 4 对应api接口 ：获取文件详情
    const { data } = await this.$axios({
      url: 'interview/show',
      params: {
        id
      }
    })
    this.one = data
  },
  methods: {
    like () {
      // 切换样式
      this.one.likeFlag = !this.one.likeFlag

      // 通知后台数据跟着变：likeFlag变 后台数据也跟着变
      this.$axios({
        method: 'POST',
        url: 'interview/opt',
        data: {
          id: this.one.id,
          optType: 1
        }
      })
      // 对当前自己操作的组件内：收藏数量变化！
      if (this.one.likeFlag) {
        this.one.likeCount++
      } else {
        this.one.likeCount--
      }
    },

    // 收藏
    collect () {
      // 切换样式
      this.one.collectFlag = !this.one.collectFlag

      // 通知后台数据跟着变：collectFlag变 后台数据也跟着变
      this.$axios({
        method: 'POST',
        url: 'interview/opt',
        data: {
          id: this.one.id,
          optType: 2 // 改变就是收藏
        }
      })
    }

  }
}
</script>

<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #FEC635;
        color: #fff;
      }
    }
  }
}
</style>
